#! /usr/bin/env python
pair_dict = {
    ')':'(',
    '}':'{',
    '\'':'\'',
    '\"':'\:',
    ']':']',
    'end':'begin'
}
def char_match(parse_string):
    special_char_list = []
    str_len = len(parse_string)
    parse_point = 0
    while(parse_point < str_len):
        #1. key word more then one chararicter
        if (parse_point + 5) <= str_len and (parse_string[parse_point:parse_point + 5] in pair_dict.values()):
            special_char_list.append(parse_string[parse_point:parse_point + 5])
            parse_point = parse_point + 5
             # the boundary is important
        elif(parse_point + 3) <= str_len and(parse_string[parse_point:parse_point + 3] in pair_dict):
            pair_key = parse_string[parse_point:parse_point + 3]
            pair_char = special_char_list.pop() 
            if pair_char != pair_dict[pair_key]:
               print "error"
               print parse_point
               break
            parse_point = parse_point + 3
        # 'left'
        elif(parse_string[parse_point] in pair_dict):
            pair_key = parse_string[parse_point]
            pair_char = special_char_list.pop() 
            if pair_char != pair_dict[pair_key]:
               print "error"
               print parse_point
               break 
            parse_point = parse_point + 1 
        #'right'
        elif(parse_string[parse_point] in pair_dict.values()):
            special_char_list.append(parse_string[parse_point])
            parse_point = parse_point + 1
        #'none'
        else:
            parse_point = parse_point + 1

    if parse_point < str_len:
        print "error"
        print parse_string
    elif special_char_list != []:
        print "error"
        print parse_string
        print special_char_list
    else:
        print "successfully"

string = "begin adfdf{d ()df}endbegin"

char_match(string)
    

    